package com.example.orderfoodsystem22201310liuziming.utils;

import net.coobird.thumbnailator.Thumbnails;

import javax.swing.*;
import java.io.IOException;

public class ThumbnailUtis {
    /**
     * @param imageUrl 原图片url
     * @param newWidth 压缩后宽度
     * @param quality  图片质量(<1)
     * @return
     */
    public static String imageZoom(String imageUrl, int newWidth, float quality,String imagePath) {
        ImageIcon imageIcon = new ImageIcon(imageUrl);
        int iconWidth = imageIcon.getIconWidth();
        int iconHeight = imageIcon.getIconHeight();
        try {
            //压缩后图片保存地址
            Integer height = (newWidth * iconHeight) / iconWidth;
            Thumbnails.of(imageUrl).size(newWidth, height).outputQuality(quality).toFile(imagePath);
            return imagePath;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}